package org.yccloud.yc.demo.kt.service

import org.springframework.stereotype.Service
import org.yccloud.yc.demo.kt.dao.MessageRepository
import org.yccloud.yc.demo.kt.model.MessagePO
import java.util.*

@Service
class MessageDBService(val db: MessageRepository) {

    fun findMessages(): List<MessagePO> = db.findAll().toList()

    fun findMessageById(id: String): List<MessagePO> = db.findById(id).toList()

    // extension db.findById
    fun <T : Any> Optional<out T>.toList(): List<T> =
        if (isPresent) listOf(get()) else emptyList()
}